package shape

import (
	"go_learn/actor"
	"math"
)

type Shape struct {
	actor.ConcurrentActor
	test1Addr uint
}

func (slf *Shape) Name() string {
	return "shape"
}

func (slf *Shape) OnInit() {
	slf.RegistHandle(actor.Normal, "circle", slf.circle)
	slf.RegistHandle(actor.Normal, "rectangle", slf.rectangle)
	slf.RegistHandle(actor.Normal, "triangle", slf.triangle)
	slf.RegistHandle(actor.Normal, "sphere", slf.sphere)
}

func (slf *Shape) Run(args interface{}) {
}

func (slf *Shape) OnDestroy() {

}

func (slf *Shape) circle(source uint, args float64) (ret interface{}, err error) {
	y, _ := slf.Call("math", actor.Normal, "square", args)
	area, _ := slf.Call("math", actor.Normal, "multiply", math.Pi, y)
	area, _ = slf.Call("math", actor.Normal, "divide", area, 2.0)
	ret = [...]interface{}{area, "圆形"}
	return
}

func (slf *Shape) rectangle(source uint, h float64, l float64) (ret interface{}, err error) {
	area, _ := slf.Call("math", actor.Normal, "multiply", h, l)
	ret = [...]interface{}{area, "矩形"}
	return
}

func (slf *Shape) triangle(source uint, a float64, h float64) (ret interface{}, err error) {
	area, _ := slf.Call("math", actor.Normal, "multiply", a, h)
	area, _ = slf.Call("math", actor.Normal, "divide", area, 2)
	ret = [...]interface{}{area, "三角形"}
	return
}

//4/3πr^3
func (slf *Shape) sphere(source uint) (ret interface{}, err error) {
	info, _ := slf.Call("test", actor.Normal, "getSphereInfo")
	constV := info.([2]interface{})[0].(float64)
	r := info.([2]interface{})[1].(float64)
	x, _ := slf.Call("math", actor.Normal, "cbrt", r)
	area, _ := slf.Call("math", actor.Normal, "multiply", constV, x)
	ret = [...]interface{}{area.(float64) * math.Pi, "球形"}
	return
}
